package com.hinotoyk.testsystem.controller;

import com.hinotoyk.testsystem.config.UserToken;
import com.hinotoyk.testsystem.entity.Student;
import com.hinotoyk.testsystem.entity.Teacher;
import com.hinotoyk.testsystem.service.AdminService;
import com.hinotoyk.testsystem.service.StudentService;
import com.hinotoyk.testsystem.service.TeacherService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @program: testsystem
 * @description:
 * @author: DYJ
 * @create: 2019-11-13 11:36
 **/
@Controller
public class LoginController {

    @Resource
    private StudentService studentService;

    @Resource
    private TeacherService teacherService;

    @RequestMapping("/index")
    public String getIndex(Model model, @ModelAttribute("student")Student student,@ModelAttribute("teacher")Teacher teacher){
        if(student.getStudentId() !=null && student.getStudentId()>0){
            model.addAttribute("student",student);
            return "stuIndex";
        }

        if(teacher.getTeacherId() !=null && teacher.getTeacherId()>0){
            model.addAttribute("teacher",teacher);
            return "teachIndex";
        }
        return "index";
    }


    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/handleLogin")
    public String handleLogin(Model model, String username, String pw, String loginType,
                              RedirectAttributes redattr, HttpServletRequest request){
        if(username==null || username.equals("")){
            model.addAttribute("msg","用户名不能为空");
            return "login";
        }
        if(pw==null || pw.equals("")){
            model.addAttribute("msg","密码不能为空");
            return "login";
        }

        UserToken token = new UserToken(username,pw,loginType);
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(token);
            if(subject.isAuthenticated()){
                if("Student".equals(loginType)){
                    Student student = studentService.getStudent(username);
                    request.getSession().setAttribute("student",student);
                    redattr.addFlashAttribute("student",student);
                }
                if("Teacher".equals(loginType)){
                    Teacher teacher = teacherService.getTeacher(username);
                    //放进session里
                    request.getSession().setAttribute("teacher",teacher);
                    redattr.addFlashAttribute("teacher",teacher);
                }

                return "redirect:/index";
            }
        }catch (Exception e){
            model.addAttribute("msg","用户名或者密码错误");
            return "login";
        }

        return "login";
    }
}
    